﻿using System.Data.Common;
using Abp.EntityFramework;
using System.Data.Entity;
using STOWaybillMall.Core;
using EntityFramework.DynamicFilters;
namespace STOWaybillMall.EntityFramework
{
    public class STOWaybillMallDbContext : AbpDbContext
    {
        //TODO: Define an IDbSet for each Entity...

        //Example:
        //public virtual IDbSet<User> Users { get; set; }

        /* NOTE: 
         *   Setting "Default" to base class helps us when working migration commands on Package Manager Console.
         *   But it may cause problems when working Migrate.exe of EF. If you will apply migrations on command line, do not
         *   pass connection string name to base classes. ABP works either way.
         */
        public STOWaybillMallDbContext()
            : base("name=STO_Waybill_Mall")
        {

        }

        /* NOTE:
         *   This constructor is used by ABP to pass connection string defined in STOWaybillMallDataModule.PreInitialize.
         *   Notice that, actually you will not directly create an instance of STOWaybillMallDbContext since ABP automatically handles it.
         */
        public STOWaybillMallDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {

        }

        //This constructor is used in tests
        public STOWaybillMallDbContext(DbConnection existingConnection)
         : base(existingConnection, false)
        {

        }

        public STOWaybillMallDbContext(DbConnection existingConnection, bool contextOwnsConnection)
         : base(existingConnection, contextOwnsConnection)
        {

        }

        public virtual DbSet<BatchExpressWaybillInfo> BatchExpressWaybillInfo { get; set; }
        public virtual DbSet<BatchExpressWaybillUseInfo> BatchExpressWaybillUseInfo { get; set; }
        public virtual DbSet<GeneralExpressWaybillInfo> GeneralExpressWaybillInfo { get; set; }
        public virtual DbSet<ImageInfo> ImageInfo { get; set; }
        public virtual DbSet<MallConfigInfo> MallConfigInfo { get; set; }
        public virtual DbSet<MangeOperationRecord> MangeOperationRecord { get; set; }
        public virtual DbSet<OfflinePaymentOrderInfo> OfflinePaymentOrderInfo { get; set; }
        public virtual DbSet<OnlinePaymentMange> OnlinePaymentMange { get; set; }
        public virtual DbSet<OnlinePaymentOrderInfo> OnlinePaymentOrderInfo { get; set; }
        public virtual DbSet<OrderBaseInfo> OrderBaseInfo { get; set; }
        public virtual DbSet<OrderExpressInfo> OrderExpressInfo { get; set; }
        public virtual DbSet<OrderGeneralReceiptInfo> OrderGeneralReceiptInfo { get; set; }
        public virtual DbSet<OrderOperationRecord> OrderOperationRecord { get; set; }
        public virtual DbSet<OrderPaymentInfo> OrderPaymentInfo { get; set; }
        public virtual DbSet<OrderProductInfo> OrderProductInfo { get; set; }
        public virtual DbSet<OrderReceiptBaseInfo> OrderReceiptBaseInfo { get; set; }
        public virtual DbSet<ProductBaseInfoImages> ProductBaseInfoImages { get; set; }
        public virtual DbSet<ProductDetailTemplate> ProductDetailTemplate { get; set; }
        public virtual DbSet<ProductDetailTemplateInfo> ProductDetailTemplateInfo { get; set; }
        public virtual DbSet<ProductInfo> ProductInfo { get; set; }
        public virtual DbSet<ProductSpecInfo> ProductSpecInfo { get; set; }
        public virtual DbSet<ProductSpecProp> ProductSpecProp { get; set; }
        public virtual DbSet<UserBaseInfo> UserBaseInfo { get; set; }
        public virtual DbSet<UserBuyProductConfig> UserBuyProductConfig { get; set; }
        public virtual DbSet<UserOperationRecord> UserOperationRecord { get; set; }
        public virtual DbSet<UserReceiptInfo> UserReceiptInfo { get; set; }
        public virtual DbSet<UserStoreInfo> UserStoreInfo { get; set; }
        public virtual DbSet<OrderAuditInfo> OrderAuditInfo { get; set; }
        public virtual DbSet<OrderCloseInfo> OrderCloseInfo { get; set; }
        public virtual DbSet<OrderSplitInfo> OrderSplitInfo { get; set; }
        public virtual DbSet<ProductBaseInfo> ProductBaseInfo { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<BatchExpressWaybillUseInfo>()
                .Property(e => e.OrderID)
                .IsUnicode(false);

            modelBuilder.Entity<GeneralExpressWaybillInfo>()
                .Property(e => e.CodeNum)
                .IsUnicode(false);

            modelBuilder.Entity<GeneralExpressWaybillInfo>()
                .Property(e => e.OrderID)
                .IsUnicode(false);

            modelBuilder.Entity<ImageInfo>()
                .Property(e => e.URI)
                .IsUnicode(false);

            modelBuilder.Entity<ImageInfo>()
                .Property(e => e.UseType)
                .IsUnicode(false);

            modelBuilder.Entity<MallConfigInfo>()
                .Property(e => e.Name)
                .IsUnicode(false);

            modelBuilder.Entity<MangeOperationRecord>()
                .Property(e => e.IP)
                .IsUnicode(false);

            modelBuilder.Entity<OfflinePaymentOrderInfo>()
                .Property(e => e.TransferCode)
                .IsUnicode(false);

            modelBuilder.Entity<OnlinePaymentOrderInfo>()
                .Property(e => e.PayNo)
                .IsUnicode(false);

            modelBuilder.Entity<OnlinePaymentOrderInfo>()
                .Property(e => e.TradeNo)
                .IsUnicode(false);

            modelBuilder.Entity<OnlinePaymentOrderInfo>()
                .Property(e => e.PayBusinessID)
                .IsUnicode(false);

            modelBuilder.Entity<OnlinePaymentOrderInfo>()
                .Property(e => e.PlatformAppId)
                .IsUnicode(false);

            modelBuilder.Entity<OnlinePaymentOrderInfo>()
                .Property(e => e.ResultCode)
                .IsUnicode(false);

            modelBuilder.Entity<OrderBaseInfo>()
                .Property(e => e.OrderID)
                .IsUnicode(false);

            modelBuilder.Entity<OrderBaseInfo>()
                .Property(e => e.TotalFee)
                .HasPrecision(19, 4);

            modelBuilder.Entity<OrderBaseInfo>()
                .Property(e => e.NeedPayFee)
                .HasPrecision(19, 4);

            modelBuilder.Entity<OrderBaseInfo>()
                .Property(e => e.BalanceOfDeduction)
                .HasPrecision(19, 4);

            modelBuilder.Entity<OrderBaseInfo>()
                .Property(e => e.SplitFormOrderID)
                .IsUnicode(false);

            modelBuilder.Entity<OrderBaseInfo>()
                .HasMany(e => e.OrderAuditInfo)
                .WithRequired(e => e.OrderBaseInfo)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<OrderBaseInfo>()
                .HasOptional(e => e.OrderCloseInfo)
                .WithRequired(e => e.OrderBaseInfo);

            modelBuilder.Entity<OrderBaseInfo>()
                .HasMany(e => e.OrderSplitInfo)
                .WithRequired(e => e.OrderBaseInfo)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<OrderExpressInfo>()
                .Property(e => e.ExpressNum)
                .IsUnicode(false);

            modelBuilder.Entity<OrderGeneralReceiptInfo>()
                .Property(e => e.Phone)
                .IsUnicode(false);

            modelBuilder.Entity<OrderGeneralReceiptInfo>()
                .Property(e => e.PostalCode)
                .IsUnicode(false);

            modelBuilder.Entity<OrderOperationRecord>()
                .Property(e => e.OrderID)
                .IsUnicode(false);

            modelBuilder.Entity<OrderPaymentInfo>()
                .Property(e => e.Payment)
                .HasPrecision(19, 4);

            modelBuilder.Entity<OrderPaymentInfo>()
                .Property(e => e.PapmentNo)
                .IsUnicode(false);

            modelBuilder.Entity<OrderPaymentInfo>()
                .Property(e => e.TradeNo)
                .IsUnicode(false);

            modelBuilder.Entity<OrderProductInfo>()
                .Property(e => e.BuyPrice)
                .HasPrecision(19, 4);

            modelBuilder.Entity<OrderProductInfo>()
                .Property(e => e.TotalFee)
                .HasPrecision(19, 4);

            modelBuilder.Entity<OrderProductInfo>()
                .Property(e => e.OrginalPrice)
                .HasPrecision(19, 4);

            modelBuilder.Entity<OrderProductInfo>()
                .Property(e => e.OrderID)
                .IsUnicode(false);

            modelBuilder.Entity<OrderProductInfo>()
                .Property(e => e.AccessoriesRate)
                .HasPrecision(8, 4);

            modelBuilder.Entity<ProductInfo>()
                .Property(e => e.GroupIDs)
                .IsUnicode(false);

            modelBuilder.Entity<ProductInfo>()
                .Property(e => e.DefaultPrice)
                .HasPrecision(19, 4);

            modelBuilder.Entity<UserBaseInfo>()
                .Property(e => e.MobileNo)
                .IsUnicode(false);

            modelBuilder.Entity<UserBaseInfo>()
                .Property(e => e.LoginPwd)
                .IsUnicode(false);

            modelBuilder.Entity<UserBuyProductConfig>()
                .Property(e => e.Price)
                .HasPrecision(19, 4);

            modelBuilder.Entity<UserOperationRecord>()
                .Property(e => e.IP)
                .IsUnicode(false);

            modelBuilder.Entity<UserReceiptInfo>()
                .Property(e => e.Phone)
                .IsUnicode(false);

            modelBuilder.Entity<UserReceiptInfo>()
                .Property(e => e.PostalCode)
                .IsUnicode(false);

            modelBuilder.Entity<OrderAuditInfo>()
                .Property(e => e.OrderID)
                .IsUnicode(false);

            modelBuilder.Entity<OrderCloseInfo>()
                .Property(e => e.OrderID)
                .IsUnicode(false);

            modelBuilder.Entity<OrderCloseInfo>()
                .Property(e => e.CloseReason)
                .IsFixedLength();

            modelBuilder.Entity<OrderCloseInfo>()
                .Property(e => e.CloseTime)
                .IsFixedLength();

            modelBuilder.Entity<OrderSplitInfo>()
                .Property(e => e.OrderID)
                .IsUnicode(false);

            modelBuilder.Entity<ProductBaseInfo>()
                .Property(e => e.Rate)
                .HasPrecision(8, 4);
        }
    }
}
